//
//  BaseTabBar.m
//  MirrorCharge
//
//  Created by dllo on 16/10/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "BaseTabBar.h"
#import "LocalJSONAnalytical.h"
#import "YYModel.h"
#import "MainTabBarModel.h"
#import "UIView+RecentVC.h"

@interface BaseTabBar ()

@property (nonatomic, strong) NSMutableArray<MainTabBarModel *> *modelArray;

@end

@implementation BaseTabBar

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self handleData];
        self.items = [self getTabBarItems];
        [self setupTabBar];
    }
    return self;
}

- (void)handleData {
    NSDictionary *modelDic = [LocalJSONAnalytical JSONAnalyticalWithFileName:@"TabBarModel"];
    _modelArray = [NSMutableArray array];
    for (NSDictionary *dic in modelDic[@"data"]) {
        MainTabBarModel *model = [MainTabBarModel yy_modelWithDictionary:dic];
        [_modelArray addObject:model];
    }
}

- (NSArray<UITabBarItem *> *)getTabBarItems {
    
    NSMutableArray<UITabBarItem *> *itemsArray = [NSMutableArray array];
    for (MainTabBarModel *model in _modelArray) {
        UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:model.title image:[UIImage imageNamed:model.noSelectImage] tag:100];
        self.delegate = self;
        [itemsArray addObject:item];
    }
    
    return itemsArray;
}

- (void)setupTabBar {
    self.tintColor = [UIColor grayColor];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSUInteger index = [tabBar.items indexOfObject:item];
    UIViewController *vc = [self getVCRecently];
    Class vcClass = NSClassFromString(_modelArray[index].vcClass);
    UIViewController *pushVC = [[vcClass alloc]init];
    
    [vc.navigationController pushViewController:pushVC animated:YES];
    
}

@end
